#include "board_base.h"

#include "bsp_uart.h"
#include "Servo.h"
#include "Car.h"
#include "ITM.h"
#include "mpu6050.h"
#include "ws_config.h"
#include "bsp_uart.h"
/*-----------------------------------------------------------------------
|                                define                                 |
-----------------------------------------------------------------------*/

LEDState_e LED_State = LED_RUN;
uint8_t rgb_data[6] = {0x00,0x00,0x00,0x00,0x00,0x00};//GRB

/*-----------------------------------------------------------------------
|                               Function                                |
-----------------------------------------------------------------------*/

/**
 * @brief 记录归零，开始测量时间
 * @param timecnt 用来记录时间的变量
 * @retval uint32_t 
 * @date 2022-11-27
 */
void bsp_MesureTimeStart(uint32_t *timecnt)
{
	*timecnt = HAL_GetTick();
}

/**
 * @brief 测量时间
 * @param old_counter 用来记录时间的变量
 * @retval uint32_t 上次使用到这次使用的时间(ms)
 * @date 2022-11-27
 */
uint32_t bsp_MesureTime(uint32_t *old_counter)
{
	uint32_t counter, couter_current;
	couter_current = HAL_GetTick();
	if (couter_current > *old_counter)//没有溢出
		counter = couter_current - *old_counter;
	else//发生过溢出
		counter = couter_current + 0XFFFFFFFF - *old_counter;
	*old_counter = couter_current;
	return (counter);
}

uint16_t TaskBuzzerID = 0;
void TaskBuzzer(const void *arg)
{
    Buzzer_write(0);
}
void Beep(uint32_t time)
{
    stimer_task_stop(TaskBuzzerID);
    stimer_task_set_interval(TaskBuzzerID, time);
    stimer_task_start(TaskBuzzerID, 1, (void*)"TaskBuzzer");
    Buzzer_write(1);
}

/**
 * @brief 板载资源初始化
 * @date 2022-11-07
 */
void board_base_init(void)
{
	UART_BSP_Init();
	CarPort_Init();
	// Car_Init();
	DWT_Init();
	WS_System_Config_Init();
    bsp_key_init();
	// TraceEN_GPIO_Port->BSRR |= TraceEN_Pin;
}


void RGB_Send(uint8_t *sdata)
{ // 发送亮度数据
    uint8_t n = 0;
    uint8_t x = 0, y = 0, z = 0;
	LED_RESET;
	DWT_delay_us(110);
    for (x = 0; x < 6; x++)
    { // 发送6字节数据；6 = 一个灯3字节数据*2个灯
        n = sdata[x];
        for (y = 0; y < 8; y++)
        {
            z = ((n << y) & 0x80);//高位先发
            if (z)//1码,T1H = [650,1000]ns,T1L = [1250,3000]ns
            {
                LED_SET; // 灯的控制脚位设置为输出高//1NOP=15ns
                DWT_delay_Tick((0.825-0.5)*72);//825/15=55
                LED_RESET;
                DWT_delay_Tick(1500); // 灯的控制脚位设置为输出低
            }
            else //0码,T0H = [200,350]ns,T0L = [1600,3000]ns
            {
                LED_SET;
                //275ns
                LED_RESET;
                DWT_delay_Tick(1800);
            }
        }
    }
	LED_SET;
}

/**
 * @brief 设置RGB颜色
 * @param LED 0、1
 * @param R 
 * @param G 
 * @param B 
 * @date 2022-11-26
 */
void RGB_SET(uint8_t LED,uint8_t R, uint8_t G, uint8_t B)
{
	if(LED > 2)return ;
	rgb_data[LED*3] = G;
	rgb_data[LED*3+1] = R;
	rgb_data[LED*3+2] = B;
}

void LED_Run(const void *arg)
{
    static uint8_t cnt = 0;
    cnt++;
	//RGB运行灯

    if(cnt % 2 == 0)
    {
        RGB_SET(1,RGBREAD(LED_State));
    }
    else
    {
        RGB_SET(1,RGBREAD(LED_OFF));
    }
	
	RGB_Send(&rgb_data[0]);

}

iSW_t hisw1[BSP_KEY_NUM] = {0};
void bsp_key_input_raed(uint8_t *results, uint16_t num)
{
    if (results == NULL || num == 0)
        return ;
    results[0] = HAL_GPIO_ReadPin(SW1_GPIO_Port, SW1_Pin);
    results[1] = HAL_GPIO_ReadPin(SW2_GPIO_Port, SW2_Pin);
    results[2] = HAL_GPIO_ReadPin(SW3_GPIO_Port, SW3_Pin);
}

void bsp_key_init(void)
{
    /*-------------------初始按键并配置功能-------------------*/
    // 初始化按键 设置触发为低电平，消抖时间为2tick(20ms)
    iSW_Init(hisw1, BSP_KEY_NUM, iSW_TRIGGER_LEVEL_LOW, 2);
    iSW_Set_Mode0(&hisw1[0], iSW_TRIGGER_WAY_PRESS);
    iSW_Set_Mode0(&hisw1[1], iSW_TRIGGER_WAY_PRESS);
    iSW_Set_Mode0(&hisw1[2], iSW_TRIGGER_WAY_PRESS);
}

